var app = getApp();
Page({
    data: {
        curPage: '/zofui_sales/pages/index/index',
        isIPX: app.globalData.isIPX ? 1 : 0,
        isindex: true,
        downget: false,
        datalist: [],
        isshowtrade: false,
        isshowcity: false,
        isshowshoplist: false,
        citylist: [],
        shoparr: [],
        isposter: 0,
        shopid: 0,
        siteroot: app.siteInfo.siteroot.replace('app/index.php', ''),
        uniacid: app.siteInfo.uniacid,
        Time: app.globalData.Time,
        iserr: 0,
        options: {},
        hidetop: false,
        otherpage: [],
        tabact: 0,
    },
    onLoad: function (options) {
        var self = this;
        wx.hideTabBar();
        self.data.options = options;
        var zfid = options.zfid ? options.zfid : (options.scene ? options.scene : 0);
        var isposter = options.scene ? 1 : 0;
        self.data.isposter = options.isposter * 1 == 1 ? 1 : isposter;
        self.setData({
            statusBarHeight: app.globalData.sysInfo ? app.globalData.sysInfo.statusBarHeight : 20,
            zfid: zfid,
        });

        var loc = wx.getStorageSync('nlloc');
        if (loc.latitude) {
            self.setData({
                lat: loc.latitude,
                lng: loc.longitude,
            })
        }
        app.com.auth(function () {
            self.setData({
                isauth: true,
            });
            self.getData(loc.latitude, loc.longitude, false);
            if (self.data.zfid > 0) {
                app.com.http('user_v1', 'POST', {
                    op: 'auth',
                    zfid: self.data.zfid
                }, 0, false, '', '', function (res) {})
            }
        });

    },
    getData(lat, lng, iscache, fn) {
        var self = this;
        var lat = lat ? lat : 11;
        var lng = lng ? lng : 11;
        var cachetime = self.data.isdown || !iscache ? 0 : 30;

        var postdata = {
            op: 'info',
            lat: lat,
            lng: lng,
            zfid: self.data.zfid,
            isnew: 1,
            isposter: self.data.isposter,
            shopid: self.data.shopid,
        }

        app.com.http('index_v1', 'POST', postdata, cachetime, false, '', function (err) {
            self.setData({
                iserr: 1,
            })
        }, function (res) {

            app.com.setBar(app, self);
            if (!res.data.errno) {
                if (res.data.data.params.isheader == 1) {
                    var weburl = encodeURIComponent(res.data.data.params.headerurl);
                    wx.navigateTo({
                        url: '/zofui_sales/pages/webview/webview?url=' + weburl,
                    });
                }

                self.setData({
                    inited: true,
                    pagedata: res.data.data.page,
                })
                self.data.sharedata = res.data.data.sharedata;
                setTimeout(function () {
                    self.setData({
                        params: res.data.data.params,
                        buyed: res.data.data.buyed ? res.data.data.buyed : false,
                        trade: res.data.data.trade ? res.data.data.trade : [],
                        citylist: res.data.data.citylist ? res.data.data.citylist : [],
                        acttrade: res.data.data.acttrade ? res.data.data.acttrade : {},
                        actcity: res.data.data.actcity ? res.data.data.actcity : {},
                        newuserlist: res.data.data.newuserlist ? res.data.data.newuserlist : [],
                        shoparr: res.data.data.shoparr ? res.data.data.shoparr : [],
                        otherpage: res.data.data.otherpage ? res.data.data.otherpage : [],
                        isshowshoplist: false,
                        iserr: 0,
                        tabact: 0,
                    }, function () {
                        if (fn && !res.data.data.isauth) {
                            fn();
                        } else {
                            self.setData({
                                isauth: true,
                            });
                        }
                        self.swiperchange();
                    });
                }, 5)

                setTimeout(function () {

                    wx.setNavigationBarTitle({
                        title: res.data.data.params.title ? res.data.data.params.title : '主页',
                    });

                    if (res.data.data.params.changepid && res.data.data.params.changepid.status == 1) {
                        app.com.confirm(res.data.data.params.changepid.tips, function () {
                            let e = {
                                currentTarget: {
                                    dataset: {
                                        id: res.data.data.params.changepid.pid,
                                    }
                                }
                            }
                            if (res.data.data.params.changepid.type == 1) {
                                self.changetrade(e);
                            } else {
                                self.changecity(e);
                            }
                        }, function () {
                            self.cancelsetcity();
                        });
                    }

                    if (res.data.data.params.idxtops == 0) {
                        let color = !res.data.data.params.idxbg || res.data.data.params.idxbg == '#ffffff' ? '#000000' : '#ffffff';
                        wx.setNavigationBarColor({
                            frontColor: color,
                            backgroundColor: color,
                        });
                    }

                    app.com.getLocation(self, function (loc) {
                        let data = res.data.data;
                        let loname = loc.name ? loc.name : false;

                        if (data.params.dlxcx == 1) {
                            loname = data.params.shopname;
                        } else {
                            if (data.params.loctype == 1) {
                                loname = data.acttrade && data.acttrade.name ? data.acttrade.name : false;
                            } else if (data.params.loctype == 2) {
                                loname = data.actcity && data.actcity.name ? data.actcity.name : false;
                            }
                        }

                        self.setData({
                            locname: loname ? loname : '',
                            lat: loc.latitude,
                            lng: loc.longitude,
                        });
                        var pdownget = self.selectComponent('#pdownget');
                        if (pdownget) pdownget.setlat(loc.latitude, loc.longitude);

                        if (!loc.name && data.params.loctype == 0 && data.params.dlxcx != 1) {
                            self.geocoder(loc.latitude, loc.longitude, data.params.tenkey);
                        }

                    }, 1);
                }, 5)

            } else {
                app.com.alert(res.data.message);
            }
        });
    },
    onShareAppMessage: function () {
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl: self.data.sharedata.img,
        }
    },
    onShareTimeline() {
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl: this.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if (this.data.isdown) return false;
        this.data.isdown = true;

        this.onLoad(this.data.options);

        var cityinfo = this.selectComponent('#cityinfo');
        if (cityinfo) cityinfo.reset('down');

        var pdownget = this.selectComponent('#pdownget');
        if (pdownget) pdownget.reset('down');

        for (var i = 0; i < this.data.pagedata.length; i++) {
            if (this.data.pagedata[i].name == 'article') {
                let articlelist = this.selectComponent('#articlelist' + i);
                if (articlelist) articlelist.reset('down');
            } else if (this.data.pagedata[i].name == 'shop') {
                let shoplist = this.selectComponent('#shop' + i);
                if (shoplist) shoplist.reset('down');
            }
        }

        this.data.isdown = false;
        wx.stopPullDownRefresh();
    },
    onReachBottom: function () {
        var self = this;
        var pdownget = this.selectComponent('#pdownget');
        if (pdownget) pdownget.load();
        var cityinfo = this.selectComponent('#cityinfo');
        if (cityinfo) cityinfo.load();

        for (var i = 0; i < this.data.pagedata.length; i++) {
            if (this.data.pagedata[i].name == 'article') {
                let articlelist = this.selectComponent('#articlelist' + i);
                if (articlelist) articlelist.load();
            } else if (this.data.pagedata[i].name == 'shop') {
                let shoplist = this.selectComponent('#shop' + i);
                if (shoplist) shoplist.load();
            }
        }

    },
    showJoin(e) {
        var self = this;
        self.setData({
            Joinanimation: !self.data.Joinanimation,
        })
    },
    showsub(e) {
        this.setData({
            showsub: true,
            subtype: e.currentTarget.dataset.type
        })
    },
    locCity() {
        var self = this;

        if (self.data.params.dlxcx == 1) {
            self.setData({
                isshowshoplist: true,
            })
        } else {
            if (self.data.params.loctype == 0 || !self.data.params.loctype) {
                wx.chooseLocation({
                    success: function (res) {
                        var now = new Date().getTime();
                        wx.setStorageSync('nlloc', {
                            latitude: res.latitude,
                            longitude: res.longitude,
                            name: res.name ? res.name : res.address,
                            expire: now + 300 * 1000
                        });
                        self.setData({
                            locname: res.name ? res.name : res.address,
                            inited: false,
                            showsub: false,
                        })
                        self.getData(res.latitude, res.longitude, false);
                    }
                });
            } else if (self.data.params.loctype == 1) {
                self.setData({
                    isshowtrade: true,
                })
            } else if (self.data.params.loctype == 2) {
                self.setData({
                    isshowcity: true,
                })
            }
        }
    },
    isshowtradefn() {
        this.setData({
            isshowtrade: false,
        })
    },
    isshowcityfn() {
        this.setData({
            isshowcity: false,
        })
    },
    isshowshopfn() {
        this.setData({
            isshowshoplist: false,
        })
    },
    changeshop(e) {
        var sid = e.currentTarget.dataset.id;
        if (sid * 1 == this.data.params.bindshopid) return false;
        this.setData({
            shopid: sid,
            'params.bindshopid': sid,
        })
        this.getData(this.data.lat, this.data.lng, false);
        app.com.setBar(app, this, sid);
    },
    changetrade(e) {
        var self = this;
        var postdata = {
            op: 'settrade',
            tradeid: e.currentTarget.dataset.id,
        }
        app.com.http('user_v1', 'POST', postdata, 0, true, '', '', function (res) {

            for (var i = 0; i < self.data.trade.length; i++) {
                if (self.data.trade[i].id == postdata.tradeid) {
                    self.setData({
                        isshowtrade: false,
                        acttrade: self.data.trade[i],
                        locname: self.data.trade[i].name,
                    })

                    self.getData(self.data.lat, self.data.lng, false);
                    return false;
                }
            }
        })
    },
    searchcity(e) {
        var self = this;
        var thisarr = [];
        if (e.detail.value) {

            for (var i = 0; i < self.data.citylist.length; i++) {
                if (self.data.citylist[i].name.indexOf(e.detail.value) >= 0) {
                    thisarr.push(self.data.citylist[i]);
                }
            }
        }
        self.setData({
            scitylist: thisarr
        })
    },
    falsecity() {
        self.setData({
            scitylist: []
        })
    },
    changecity(e) {
        var self = this;
        var postdata = {
            op: 'setcity',
            cityid: e.currentTarget.dataset.id,
        }
        app.com.http('user_v1', 'POST', postdata, 0, true, '', '', function (res) {

            for (var i = 0; i < self.data.citylist.length; i++) {
                if (self.data.citylist[i].pid == postdata.cityid) {
                    self.setData({
                        isshowcity: false,
                        actcity: self.data.citylist[i],
                        locname: self.data.citylist[i].name,
                    })

                    self.getData(self.data.lat, self.data.lng, false);
                    return false;
                }
            }
        })
    },
    cancelsetcity() {
        var self = this;
        var postdata = {
            op: 'cancelsetcity',
        }
        app.com.http('user_v1', 'POST', postdata, 0, false, '', '', function (res) {})
    },
    geocoder(lat, lng, key) {
        var self = this;
        var QQMapWX = require('../../resource/js/qqmap-wx-jssdk.min.js');
        var qqmapsdk = new QQMapWX({
            key: key // 必填
        });
        //var lat = -225.969815359375;
        //var lng = -21.47933613442282;
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: lat,
                longitude: lng
            },
            success: function (res) {
                let city = res.result.address_component.district ? res.result.address_component.district : res.result.address_component.city;
                self.setData({
                    locname: city ? city + res.result.address_component.street_number : '',
                });
            },
            fail: function (res) {
                console.log(res)
            }
        })
    },
    footNavigate(e) {
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
            url: url
        })
    },
    chmove() {

    },
    location: function (e) {
        app.com.location(e);
    },
    otherapp: function (e) {
        app.com.otherapp(e);
    },
    toweburl: function (e) {
        app.com.toweburl(e, this);
    },
    navigateto: function (e) {
        var self = this;
        if (self.data.params.indexmes == 1 || !wx.requestSubscribeMessage) {
            app.com.navigateto(e);
        } else {
            wx.requestSubscribeMessage({
                tmplIds: self.data.params.submess,
                complete: function () {
                    app.com.navigateto(e);
                }
            })
        }
    },
    redirectto: function (e) {
        app.com.redirectto(e);
    },
    callphone: function (e) {
        app.com.callphone(e);
    },
    other: function (e) {
        app.com.otherapp(e);
    },
    weburl: function (e) {
        app.com.toweburl(e, this);
    },
    url: function (e) {
        app.com.navigateto(e);
    },
    tel: function (e) {
        app.com.callphone(e);
    },
    copy(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.copy
        })
    },
    onPageScroll(e) {
        var self = this;
        if (this.data.params.idxtops == 2 || this.data.params.idxtops == 3 || this.data.params.idxtops == 4) {
            if (e.scrollTop * 1 > 100 && !this.data.showtophead) {
                this.setData({
                    showtophead: true,
                    hidetop: true,
                }, function () {
                    self.setData({
                        hidetop: false,
                    })
                    wx.setNavigationBarColor({
                        frontColor: '#ffffff',
                        backgroundColor: '#ffffff',
                    });
                })
            }
            if (e.scrollTop * 1 <= 100 && this.data.showtophead) {
                this.setData({
                    showtophead: false,
                    hidetop: true,
                }, function () {
                    self.setData({
                        hidetop: false,
                    })
                    wx.setNavigationBarColor({
                        frontColor: '#000000',
                        backgroundColor: '#ffffff',
                    });
                })
            }
        }
    },
    FormIdsubmit: function (e) {
        if (app.globalData.formarr.length <= 20) app.globalData.formarr.push(e.detail.formId);
    },
    swiperchange: function (e) {
        this.animationaa = wx.createAnimation({
            duration: 0,
        })
        this.animationaa.opacity(0).step();
        this.setData({
            animationaa: this.animationaa.export()
        })

        this.animationaa = wx.createAnimation({
            duration: 2000,
        })
        this.animationaa.opacity(1).step();
        this.setData({
            //输出动画
            animationaa: this.animationaa.export()
        })
    },
    changetab(e) {
        var self = this;
        var index = e.currentTarget.dataset.index * 1;
        if (index == self.data.tabact) return false;

        var postdata = {
            op: 'info',
            pid: self.data.otherpage[index].id,
        }
        app.com.http('index_v1', 'POST', postdata, 0, true, '', '', function (res) {
            if (res.data && res.data.errno == 0) {
                self.setData({
                    tabact: index,
                    pagedata: res.data.data.page,
                })
            }
        })

    },
    // 立即购买。直接支付商品
    buyit(){
        console.log('支付了');
    },
    // 跳转到商家详情
    goShop(){
        console.log('goShop');
    },
    
})